var ImportModalDemo = angular.module('FusionStorUI.import', []);
ImportModalDemo.controller('ImportModalCtrl', ['$scope','$modal', '$http', function($scope, $modal, $http) {
    var modalInstance;
    $scope.templateData = '';
    $scope.open = function(url) {  
        $scope.item = {};
        $http.get(url).success(function(data){
            $scope.templateData = data;
            modalInstance = $modal.open({  
                template : $scope.templateData,
                controller : ImportModalInstanceCtrl,  
                resolve : { 
                    item : function() {  
                        return $scope.item;  
                    }  
                }  
            });      
        });  
    }; 
    $scope.close = function() {
        modalInstance.dismiss('cancel');  
    }
}]); 
var ImportModalInstanceCtrl = function($scope, $modalInstance, item) {  
    $scope.item = item;  
    $scope.selected = {  
        item : $scope.item 
    };  
    
    
    $scope.ok = function() { 
        var this_file = $("#import_modal").find("#import_file");
        var filename = this_file.val();
        if (filename.substr(-4) == ".tar"){
            this_file.removeClass("file_color");
            $("#import_form").ajaxSubmit({
                 type: 'post',  
                 url: "/import" ,  
                 success: function(data){ 
                    if (data.reply.is_success){
                        SelfAlert("导入成功","提示");
                        $modalInstance.close($scope.selected.item);
                    }else {
                        error =  data.reply.error;
                        SelfAlert(error)
                    }
                },  
                error: function(a, XmlHttpRequest, textStatus, errorThrown){  
                    form_validation=true;           
                }                             
            })
        }
        else{
            this_file.siblings(".error_tip").text("文件名的格式应为*.tar").css({"display":"block","color":'#000'});
            this_file.addClass("file_color"); 
        }
    };  
    $scope.cancel = function() {  
        $modalInstance.dismiss('cancel');  
    };  
};   
